package face.pay.rocket;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import face.pay.entity.SharingOrderInfo;
import face.pay.rocket.listener.CallbackMessageListener;
import face.pay.rocket.listener.ProfitMessageListener;
import face.pay.rocket.listener.SharingAccountListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

//项目中加上 @Configuration 注解，这样服务启动时consumer也启动了
@Configuration
public class ConsumerClient {

    @Autowired
    private MqConfig mqConfig;

    @Autowired
    private ProfitMessageListener messageListener;

    @Autowired
    CallbackMessageListener callbackMessageListener;

    @Autowired
    SharingAccountListener sharingAccountListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean buildConsumer() {

        ConsumerBean consumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqPropertie();
        properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroupid());
        //将消费者线程数固定为20个 20为默认值
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "20");
        consumerBean.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<Subscription, MessageListener>();

        Subscription subscription = new Subscription();
        subscription.setTopic(mqConfig.getOrderTopic());
        subscription.setExpression(mqConfig.getProfitTag());
        subscriptionTable.put(subscription, messageListener);
        //订阅多个topic如上面设置

        Subscription callBackSubscription = new Subscription();
        callBackSubscription.setTopic(mqConfig.getCallBackOrderTopic());
        callBackSubscription.setExpression(mqConfig.getBackTag());

        subscriptionTable.put(callBackSubscription, callbackMessageListener);

        Subscription sharingSubscription = new Subscription();
        sharingSubscription.setTopic(mqConfig.getSharingTopic());
        sharingSubscription.setExpression(mqConfig.getSharingTag());

        subscriptionTable.put(sharingSubscription, sharingAccountListener);

        consumerBean.setSubscriptionTable(subscriptionTable);
        return consumerBean;
    }

}
